---
title: "Kubenetes Ingress 暴露TCP和UDP服务"
date: 2019-7-22
categories:
- devops
- kubernetes
tags:
---

<div id="content">
<blockquote>
<p>
<a href="https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/">https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/</a>
</p>
</blockquote>
<p>
Ingress Controller 使用 –tcp-services-configmap –udp-services-configmap 来指向一个 configmap。
这两个标志是ingress controller的启动参数，
</p>
<p>
configmap中的key为外部端口，value为要暴露的服务（格式为&lt;namespace/service name&gt;:&lt;service port&gt;:[PROXY]:[PROXY]）
</p>
<p>
例如下面的configmap，通过9000端口访问example-go（example-go监听端口8080）
</p>
<div class="org-src-container">
<pre class="src src-xml">apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-services
  namespace: ingress-nginx
data:
  9000: "default/example-go:8080"
</pre>
</div>
<p>
ingress deployment中的启动参数：
</p>
<div class="org-src-container">
<pre class="src src-yaml">spec:
  template:
     spec:
       containers:
	 - name: nginx-ingress-internal-controller
	   args:
	     - /nginx-ingress-controller
	     - '--election-id=ingress-controller-leader-internal'
	     - '--ingress-class=nginx-internal'
	     - '--configmap=ingress/nginx-ingress-internal-controller'
	     - '--tcp-services-configmap=default/tcp-services'
	     - '--udp-services-configmap=default/udp-services'
</pre>
</div>
<p>
If TCP/UDP proxy support is used, then those ports need to be exposed in the Service defined for the Ingress.
</p>
<div class="org-src-container">
<pre class="src src-yaml">apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  type: LoadBalancer
  ports:
    - name: http
      port: 80
      targetPort: 80
      protocol: TCP
    - name: https
      port: 443
      targetPort: 443
      protocol: TCP
    - name: proxied-tcp-9000
      port: 9000
      targetPort: 9000
      protocol: TCP
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
</pre>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2019-7-22</p>
<p class="author">Author: amo</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
